class PropView extends egret.Sprite {
    public constructor(type: number) {
        super();
        this._type = type;
        this.init();
    }

    // 道具元素
    private _view_box: egret.Bitmap;        // 道具盒子
    private _view_activate: egret.Bitmap;   // 激活道具图像
    private _numText: egret.BitmapText;     // 数字文本
    private _type: number = -1;             // 道具类型

    public id: number = -1;

    public get proptype(): number {
        return this._type;
    }

    private init() {
        this.createView();
        this.createNumText();
        this.addChild(this._view_activate);
        this.addChild(this._view_box);
        this.addChild(this._numText);
        this.setActivateState(true);
    }

    private createView() {
        let _interval: number = 15;
        let _width: number = (GameData.stageW - _interval * 6) / 5;
        if (!this._view_activate) {
            this._view_activate = new egret.Bitmap();
            this._view_activate.texture = RES.getRes(this.getActivateTexture(this._type));
            this._view_activate.width = _width;
            this._view_activate.height = _width;
        }
        if (!this._view_box) {
            this._view_box = new egret.Bitmap();
            this._view_box.texture = RES.getRes("propbox_png");
            this._view_box.width = this._view_activate.width + 10;
            this._view_box.height = this._view_activate.height + 10;
            this._view_box.x = -5;
            this._view_box.y = -5;
        }
    }

    private createNumText() {
        this._numText = new egret.BitmapText();
        this._numText.font = RES.getRes("number_fnt");
        this._numText.x = this._view_activate.width - 31;
    }

    private _num: number = 0; // 数量
    public get num(): number {
        return this._num;
    }
    public set num(value: number) {
        this._num = value;
        this._numText.text = value.toString();
        if (value <= 0) {
            this.setActivateState(false);
        } else {
            this.setActivateState(true);
        }
    }

    private setActivateState(value: boolean) {
        this.touchEnabled = value;
        if (value) {
            this._view_activate.texture = RES.getRes(this.getActivateTexture(this._type));
            this._view_box.texture = RES.getRes("propbox_png");
            this._numText.font = RES.getRes("number_fnt");
        } else {
            this._view_activate.texture = RES.getRes(this.getDisableTexture(this._type));
            this._view_box.texture = RES.getRes("propboxdisable_png");
            this._numText.font = RES.getRes("numberdisable_fnt");
        }
    }

    private getActivateTexture(type: number) {
        let texturenamr: string = "";
        switch (type) {
            case 0: texturenamr = "tongse_png"; break;
            case 1: texturenamr = "zhadan_png"; break;
            case 2: texturenamr = "zhenghang_png"; break;
            case 3: texturenamr = "zhenglie_png"; break;
            case 4: texturenamr = "chanzi_png"; break;
            default: break;
        }
        return texturenamr;
    }

    private getDisableTexture(type: number) {
        let texturenamr: string = "";
        switch (type) {
            case 0: texturenamr = "tongsedisable_png"; break;
            case 1: texturenamr = "zhadandisable_png"; break;
            case 2: texturenamr = "zhenghangdisable_png"; break;
            case 3: texturenamr = "zhengliedisable_png"; break;
            case 4: texturenamr = "chanzidisable_png"; break;
            default: break;
        }
        return texturenamr;
    }

    public setFocus(value: boolean) {
        if (value) {
            this._view_box = RES.getRes("propboxactive_png");
        } else {
            this._view_box = RES.getRes("propbox_png");
        }
    }
}